<template>
  <div>
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录账户设置">
          <!-- 输入框区 -->
          <el-row>
            <el-form
              label-width="300px"
              style="margin-left: 50px"
              :model="formdata"
              :rules="rules"
              ref="FROM"
            >
              <el-form-item label="用户名" prop="username">
                <el-input style="width: 300px" v-model="formdata.username" />
              </el-form-item>
              <!-- 

   -->
              <el-form-item label="密码" prop="password">
                <el-input
                  type="password"
                  style="width: 300px"
                  v-model="formdata.password"
                />
              </el-form-item>
              <!-- 

   -->
              <el-form-item>
                <el-button type="primary" size="small" @click="saveUser"
                  >保存</el-button
                >
              </el-form-item>
            </el-form>
          </el-row>
        </el-tab-pane>
        <!-- 

         -->
        <el-tab-pane label="个人详情">
          <el-row type="flex" justify="end">
            <el-tooltip content="打印个人基本信息">
              <router-link :to="`/employees/print/${userId}?type=personal`">
                <i class="el-icon-printer"></i>
              </router-link>
            </el-tooltip>
          </el-row>
          <!-- 引入组件使用动态组件 -->
          <!-- <component :is="UserComponent"/> -->
          <user-info />
        </el-tab-pane>
        <!-- 

         -->
        <el-tab-pane label="岗位信息">
          <!-- 打印岗位信息 -->
             <el-row type="flex" justify="end">
            <el-tooltip content="打印岗位信息">
              <router-link :to="`/employees/print/${userId}?type=job`">
                <i class="el-icon-printer"></i>
              </router-link>
            </el-tooltip>
          </el-row>
          <!-- 动态组件 -->
          <component :is="JobComponent"/>

          </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 引入获取用户1的基本信息
import { userMessage } from "@/api/user";
// 引入保存用户信息的接口
import { saveUserDetailById } from "@/api/employees";
// 引入组件个人详情
import UserInfo from "./components/user-info.vue";
// 引入组件岗位信息
import JobInfo from "./components/job-info.vue";

export default {
  // 注册组件
  // 局部注册的组件
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      // 从请求的网页头部获取网页的地址
      userId: this.$route.params.id,
      // 表单验证
      formdata: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 9, message: "密码长度6-9位", trigger: "blur" },
        ],
      },
      // 组件
      UserComponent: "user-info",
          JobComponent: 'job-info',
    };
  },
  created() {
    this.username();
  },
  methods: {
    // 拿到数据显示在页面上
    async username() {
      const res = await userMessage(this.userId);
      this.formdata = res;
      // this.formdata.passwordTOW=res.password
    },
    // 点击保存更改的数据
    async saveUser() {
      try {
        // 验证表单的正确性
        await this.$refs.FROM.validate();
        // this.formdata.passwrod= this.formdata.passwordTOW
        await saveUserDetailById({
          ...this.formdata,
          password: this.formdata.password,
        });
        this.$message.success("保存成功");
      } catch (eer) {
        console.log(eer);
      }
    },
  },
};
</script>

<style>
/* .aa{
  transform: translateX(50%);
} */
</style>